package com.lituoxy.dialer.thread;

import android.util.Log;

import com.huawei.rcs.caasomp.CaasOmp;
import com.huawei.rcs.caasomp.CaasOmpCfg;
import com.huawei.rcs.login.LoginApi;
import com.huawei.rcs.login.LoginCfg;
import com.huawei.rcs.login.UserInfo;

/**
 * 作者：许航晨 on 2017/7/11 15:41
 * 邮箱：948340983@qq.com
 */

public class LoginThread implements Runnable {
    private final String huaweiPhone;
    private final String password;

    public LoginThread(String huaweiPhone, String password) {
        this.huaweiPhone = huaweiPhone;
        this.password = password;
    }

    public static String truncateHeadString(String origin, int count) {
        if (origin == null || origin.length() < count) {
            return null;
        }

        char[] arr = origin.toCharArray();
        char[] ret = new char[arr.length - count];
        for (int i = 0; i < ret.length; i++) {
            ret[i] = arr[i + count];
        }

        return String.copyValueOf(ret);
    }

    @Override
    public void run() {
        LoginApi.setConfig(LoginApi.CONFIG_MAJOR_TYPE_DM_IP, LoginApi.CONFIG_MINOR_TYPE_DEFAULT, "117.78.29.66");//商用 117.78.29.66  测试用 218.4.33.55
        LoginApi.setConfig(LoginApi.CONFIG_MAJOR_TYPE_DM_PORT, LoginApi.CONFIG_MINOR_TYPE_DEFAULT, "443");
        CaasOmpCfg.setString(CaasOmpCfg.EN_OMP_CFG_SERVER_IP, "205.177.226.80");
        CaasOmpCfg.setUint(CaasOmpCfg.EN_OMP_CFG_SERVER_PORT, Integer.parseInt("8543"));
        UserInfo userInfo = new UserInfo();
        userInfo.countryCode = "+86";
        userInfo.username = truncateHeadString(huaweiPhone, 3);//"78887001100";//
        userInfo.password = password;//"Lituo@00";//"Huawei@12";//
        CaasOmpCfg.setString(CaasOmpCfg.EN_OMP_CFG_USER_NAME, huaweiPhone);//huaweiPhone "+8678887001100"
        CaasOmpCfg.setString(CaasOmpCfg.EN_OMP_CFG_PASSWORD, userInfo.password);
        LoginCfg loginCfg = new LoginCfg();
        loginCfg.isAutoLogin = true;
        loginCfg.isRememberPassword = true;//是否记住密码
        loginCfg.isVerified = false;

        Log.e("123", "进行登录 账号 CaasOmpCfg.getString(CaasOmpCfg.EN_OMP_CFG_USER_NAME) =" + CaasOmpCfg.getString(CaasOmpCfg.EN_OMP_CFG_USER_NAME));
        Log.e("123", "进行登录 密码 CaasOmpCfg.getString(CaasOmpCfg.EN_OMP_CFG_PASSWORD) =" + CaasOmpCfg.getString(CaasOmpCfg.EN_OMP_CFG_PASSWORD));
        login(userInfo, loginCfg);
        Log.e("123", "登录 login(userInfo, loginCfg) 以执行 ");
    }

    //登录方法
    private void login(UserInfo userInfo, LoginCfg loginCfg) {
        CaasOmp.addPlugin(CaasOmp.OMP_DFT_PRIORITY);
        //CaasOmpCfg.setString(CaasOmpCfg.EN_OMP_CFG_APP_KEY, "svAn4y4DiliL3JEOEhk3F9pnF3nG");
        //设置不需要key
        CaasOmp.rmvPlugin(CaasOmp.OMP_DFT_PRIORITY);
        //封装的访问方法
        LoginApi.login(userInfo, loginCfg);
    }
}